﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Media;
using System.IO;

namespace Compass
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<string> lstMain = new List<string>();
        private Random random = new Random();
        private DispatcherTimer timer = new DispatcherTimer();
        private bool isDisplaying = false;
        private int currentIndex = 0;
        private MediaPlayer mediaPlayer = new MediaPlayer();
        public MainWindow()
        {
            InitializeComponent();
            LoadConfig();
            InitializeBackground();
            InitializeMusic();
            InitializeTimer();
            InitializeUI();
        }

        private void LoadConfig()
        {
            string configPath = "./data/dat.conf";
            if (File.Exists(configPath))
            {
                lstMain = new List<string>(File.ReadAllLines(configPath));
            }
            else
            {
                MessageBox.Show("配置文件 dat.conf 未找到。");
                Application.Current.Shutdown();
            }
        }

        private void InitializeBackground()
        {
            //BackgroundImage.Source = new BitmapImage(new Uri("pack://application:,,,/imgBgd.png"));
        }

        private void InitializeMusic()
        {
            mediaPlayer.Open(new Uri("data/sndBgd.mp3", UriKind.Relative));
            mediaPlayer.MediaEnded += (s, e) => mediaPlayer.Play();
            mediaPlayer.Volume = 0.5;
            mediaPlayer.Play();
        }

        private void InitializeUI()
        {
            DisplayText.Text = "开始";
            //DisplayText.FontSize = ActualHeight / 3;
        }

        private void InitializeTimer()
        {
            timer.Interval = TimeSpan.FromSeconds(0.1); // 0.2秒切换一次，相当于每秒切换5次
            timer.Tick += Timer_Tick;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            currentIndex = random.Next(lstMain.Count);
            DisplayText.Text = lstMain[currentIndex];
        }

        private void Window_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (isDisplaying == false)
            {
                if (lstMain.Count == 0)
                {
                    DisplayText.Text = "结束!";
                }
                else
                {
                    isDisplaying = true;
                    timer.Start();
                }
                
            }
            else
            {
                isDisplaying= false;
                timer.Stop();
                if (lstMain.Count > 0)
                {
                    lstMain.RemoveAt(currentIndex);
                }
            }
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.Close(); // 关闭窗口，从而退出程序
            }
        }
    }
}
